﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using AForge.Robotics.Lego;

namespace nxt
{
    public partial class Manualy_controller : Form
    {
        Control robot;
        string cPTemp = "";
        string[] cP = new string[2];

        public Manualy_controller()
        {
            InitializeComponent();
        }

                    

        private void button1_Click(object sender, EventArgs e)
        {
            StreamReader rs;

            bool nxtConnected_1 = false; //felső nxt
            bool nxtConnected_2 = false; //alsó nxt

            try
            {
                rs = new StreamReader("comPort.nxt");
                while (rs.EndOfStream == false)
                {
                    cPTemp = rs.ReadLine();
                }
                rs.Close();

                cP = cPTemp.Split(',');
            }
            catch
            {
                label1.Text = "Status: COM PORT reading error!";
            }

            robot = new Control(cP[0], cP[1]);

            label1.Text = "Status: Nxt_1 COM PORT: " + cP[0] + ", Nxt_2 COM PORT: " + cP[1];

            try
            {
                label1.Text = "Status: Connecting to Nxt_1 and Nxt_2...";

                robot.Connect();

                //felső nxt
                if (robot.nxtConnectionStatusFront() == true)
                {
                    nxtConnected_1 = true;

                    label1.Text = "Status: Nxt_1 is connected!";
                }
                else
                {
                    label1.Text = "Status: Nxt_1 is not connected!";
                }

                //alsó nxt
                if (robot.nxtConnectionStatusBack() == true)
                {
                    nxtConnected_2 = true;

                    label1.Text = "Status: Nxt_2 is connected!";
                }
                else
                {
                    label1.Text = "Status: Nxt_2 is not connected!";
                }

                //felső-alsó
                if (nxtConnected_1 == true && nxtConnected_2 == true)
                {
                    label1.Text = "Status: Connected! Ready to go!";
                }
            }
            catch
            {
                label1.Text = "Status: Connection failed to Nxt!";
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                robot.forward(20, 100, 180);
                
            }
            catch (Exception)
            { label1.Text = "Status: Going forward failed!"; }
        }

        private void button9_Click(object sender, EventArgs e)
        {
                try
                {
                    robot.robotic_arm_close_open(false);
                    
                }
                catch (Exception)
                { label1.Text = "Status: Ball catching failed!"; }
            

        }

        private void button6_Click(object sender, EventArgs e)
        {
            try
            {
                robot.up(20, 100, 180);
                
            }
            catch (Exception)
            { label1.Text = "Status: Going up failed!"; }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            try
            {
                robot.down(20, 100, 180);
               
            }
            catch (Exception)
            { label1.Text = "Status: Going down failed!"; }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            try
                {
                    robot.backward(20, 100, 180);
                    
                }
                catch (Exception)
                { label1.Text = "Status: Going backward failed!"; }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            try
                {
                    robot.left(20, 100, 180);
                    
                }
                catch (Exception)
                { label1.Text = "Status: Going left failed!"; }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
                {
                    robot.right(20, 100, 180);
                    
                }
                catch (Exception)
                { label1.Text = "Status: Going right failed!"; }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            try
            {
                robot.Disconnect();
                label1.Text = "Disconnected!";
            }
            catch
            {
                //
            }
        }

        private void button10_Click(object sender, EventArgs e)
        {
            try
            {
                robot.robotic_arm_close_open(true);
                
            }
            catch (Exception)
            { label1.Text = "Status: Ball catching failed!"; }
        }

        private void button15_Click(object sender, EventArgs e)
        {
            try
            {
                robot.moveBackMotor(NXTBrick.Motor.A, 20, 50);
            }
            catch { }
        }

        private void button11_Click(object sender, EventArgs e)
        {
            try
            {

                robot.moveFrontMotor(NXTBrick.Motor.A, 20, 50);
            }
            catch { }
        }

        private void button12_Click(object sender, EventArgs e)
        {
            try
            {
                robot.moveFrontMotor(NXTBrick.Motor.A, -20, 50);
            }
            catch { }
        }

        private void button13_Click(object sender, EventArgs e)
        {
            try
            {
                robot.moveFrontMotor(NXTBrick.Motor.B, 20, 50);
            }
            catch { }
        }

        private void button14_Click(object sender, EventArgs e)
        {
            try
            {
                robot.moveFrontMotor(NXTBrick.Motor.B, -20, 50);
            }
            catch { }
        }

        private void button16_Click(object sender, EventArgs e)
        {
            try
            {
                robot.moveBackMotor(NXTBrick.Motor.A, -20, 50);
            }
            catch { }
        }

        private void button17_Click(object sender, EventArgs e)
        {
            try
            {
                robot.moveBackMotor(NXTBrick.Motor.B, 20, 50);
            }
            catch { }
        }

        private void button18_Click(object sender, EventArgs e)
        {
            try
            {
                robot.moveBackMotor(NXTBrick.Motor.B, -20, 50);
            }
            catch { }
        }
        
        private void button2_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.W)
            {
              try
              {
                robot.forward(20, 100, 180); 
              }
              catch (Exception)
              { label1.Text = "Status: Going forward failed!"; }
            }
            if (e.KeyCode == Keys.S)
            {
                try
                {
                    robot.backward(20, 100, 180);
                }
                catch (Exception)
                { label1.Text = "Status: Going forward failed!"; }
            }
            if (e.KeyCode == Keys.D)
            {
                try
                {
                    robot.right(20, 100, 180);
                }
                catch (Exception)
                { label1.Text = "Status: Going forward failed!"; }
            }
            if (e.KeyCode == Keys.A)
            {
                try
                {
                    robot.left(20, 100, 180);
                }
                catch (Exception)
                { label1.Text = "Status: Going forward failed!"; }
            }
            if (e.KeyCode == Keys.R)
            {
                try
                {
                    robot.up(20, 100, 180);

                }
                catch (Exception)
                { label1.Text = "Status: Going up failed!"; }
            }
            if (e.KeyCode == Keys.F)
            {
                try
                {
                    robot.down(20, 100, 180);

                }
                catch (Exception)
                { label1.Text = "Status: Going up failed!"; }
            }
            if (e.KeyCode == Keys.Space)
            {
                try
                {
                    robot.robotic_arm_close_open(false);
                }
                catch (Exception)
                { label1.Text = "Status: Ball catching failed!"; }
            }
            if (e.KeyCode == Keys.B)
            {
                try
                {
                    robot.robotic_arm_close_open(true);
                }
                catch (Exception)
                { label1.Text = "Status: Ball catching failed!"; }
            }
        }
    }
}
